package com.dotndot.view.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dotndot.MomApplication;
import com.dotndot.R;
import com.dotndot.controller.Server.ReceiveServerParam;
import com.dotndot.controller.Server.SendServerParam;
import com.dotndot.controller.network.resData.ResGson;
import com.dotndot.controller.network.resData.Response;
import com.dotndot.view.widget.BaseDialog;

public class AddStory extends BaseActivity implements OnCheckedChangeListener
{
    private final String STORY_PUBLIC_TYPE = "public";
    private final String STORY_PRIVATE_TYPE = "private";
    
    private final int GET_GALLERY_IMAGE = 0;
    
    private ImageView mAddImageBtn;
    private Button mUploadBtn;
    private CheckBox mLockBtn;
    private EditText mContent;
    private TextView mLockText;
    private Uri mImageUri = null;
    private Bitmap resizeBitmap = null;
    private String lockType = STORY_PUBLIC_TYPE;
    
    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_story);

        viewInit();
    }

    @Override
    public void viewInit() 
    {
        mAddImageBtn = (ImageView) findViewById(R.id.add_story_upload_image);
        mAddImageBtn.setOnClickListener(this);
        mContent = (EditText) findViewById(R.id.add_story_content);
        mUploadBtn = (Button) findViewById(R.id.add_story_upload_btn);
        mUploadBtn.setOnClickListener(this);
        mLockBtn = (CheckBox) findViewById(R.id.add_story_lock_btn);
        mLockBtn.setOnCheckedChangeListener(this);
        mLockText = (TextView) findViewById(R.id.add_story_lock_text);
    }

    @Override
    public void onCheckedChanged( CompoundButton buttonView, boolean isChecked )
    {
        if( buttonView.getId() == R.id.add_story_lock_btn )
        {
            if(isChecked){
                lockType = STORY_PRIVATE_TYPE;
                mLockText.setText(R.string.lock_private_type);
            }else{
                lockType = STORY_PUBLIC_TYPE;
                mLockText.setText(R.string.lock_public_type);
            }
        }
    }
    
    @Override
    public void receiveServerMsg( Message msg )
    {
        ReceiveServerParam recvData = (ReceiveServerParam) msg.obj;

        switch( recvData.getAPI() )
        {
            case STORY_ADD_API:
                
                String resJson = (String) recvData.getData();
                ResGson resGson = new ResGson(resJson);
                
                if( resGson.status.equals(Response.Status.OK.name()) )
                {
                    Toast.makeText(AddStory.this, R.string.story_add_success, Toast.LENGTH_LONG).show();
                    finish();
                }else{
                    BaseDialog.getIns().showDialog(AddStory.this, R.string.network_unknown_error);
                }
                break;
        }
    }

    @Override
    protected void onActivityResult( int requestCode, int resultCode, Intent data )
    {
        if( resultCode == RESULT_OK )
        {
            if( requestCode == GET_GALLERY_IMAGE )
            { 
//                final Bundle extras = data.getExtras();
//                if(extras != null)
//                {
//                  Bitmap photo = extras.getParcelable("data");
//                  mAddImageBtn.setImageBitmap(photo);
//                }
                
                mImageUri = data.getData();
//                resizeBitmap = utilAPI.inSampleSizeBitmapLoadFromUri(this, mImageUri, MomApplication.RESIZE_IMAGE_WIDTH, MomApplication.RESIZE_IMAGE_HEIGHT);
                resizeBitmap = utilAPI.resizeBitmapLoadFromUri(this, mImageUri, MomApplication.RESIZE_IMAGE_WIDTH, MomApplication.RESIZE_IMAGE_HEIGHT);
                if(resizeBitmap != null){
                    mAddImageBtn.setImageBitmap(resizeBitmap);
                }
            }
        }
    }

    @Override
    public void onClick( View v )
    {
        switch( v.getId() )
        {
            case R.id.add_story_upload_image:
                Intent intent;
                intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
//                intent.putExtra("crop", "true");
//                intent.putExtra("outputX", 100);
//                intent.putExtra("outputY", 100);
//                intent.putExtra("aspectX", 1);
//                intent.putExtra("aspectY", 1);
//                intent.putExtra("scale", true);
                startActivityForResult(intent, GET_GALLERY_IMAGE);
                break;

            case R.id.add_story_upload_btn:
                if(mContent.getText().toString().length() == 0 && mImageUri == null){
                    BaseDialog.getIns().showDialog(AddStory.this, R.string.story_empty_item);
                }else{
                    upload();
                }
                break;
        }
    }

    private void upload()
    {
         SendServerParam param = new SendServerParam(REQUEST.STORY_ADD_API);
         param.put(STORY.userIndex, momApp.getUserInfo().getUserIndex());
         
         if( mContent.getText().toString().length() != 0 )
         {
             param.put(STORY.content, mContent.getText().toString());
         }
        
         if( resizeBitmap != null )
         {
             param.putBitmap(STORY.image, resizeBitmap);
         }
         
         param.put(STORY.viewType, lockType);
         requestNetwork(param);
    }
}
